<?php

namespace App\Http\Controllers\admin;
use DB;
use Illuminate\Http\Request;
use App\Http\Requests\UserInsertRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Hash;
class orderController extends Controller
{
    //后台用户列表
    public function getIndex(Request $request)
    {
        
        
       
        
            $orders =DB::table('order')
                ->join('order-info','order.id','=','order-info.order-id')
                ->join('address','order.address-id','=','address.id')
                
                ->get();
            
            //dd($orders);
        return view('admin.order.index',['orders'=>$orders]);
    }


    //后台用户添加页面
    public function getAdd()
    {
        return view('admin.user.add');
    }

    //进行数据的添加操作
    public function postInsert(UserInsertRequest $request)
    {
        //提取数据
        $data = $request->only(['username','password','email','phone']);
        //处理token
        $data['token'] = str_random(50);
        //处理密码
        $data['password'] = Hash::make($data['password']);
        //文件上传
       $data['pic'] = $this->upload($request,'pic');

        $res = DB::table('user')->insert($data);
        // dd($request->all());
       if($res){
        //上传成功
        return redirect('/admin/user/index')->with('success','用户添加成功');
       }else{
        //上传失败
        return back()->with('error','用户添加失败');
       }
    }

    //封装方法 文件上传
    private function upload($request,$filename)
    {
       if($request->hasFile($filename)){
            //获取文件后缀名
        $suffix = $request->file($filename)->getClientOriginalExtension();
            //随机文件名
        $name = md5(time().rand(1,9999));
        $request->file($filename)->move('./uploads/',$name.'.'.$suffix);
        return '/uploads/'.$name.'.'.$suffix;
       } else{
        echo '没有文件上传';
       }
    }


    //封装方法 进行ajax删除
    public function postDelete(Request $request)
    {
        $id = $request->input('id');

        $res = DB::table('user')->delete($id);
        // dd($res);
        echo $res;
    }



    //封装方法 进行修改显示
    public function getEdit(Request $request)
    {
        //获取id
        $id = $request->input('id');

        //查询单条数据
        $users = DB::table('user')->where('id',$id)->first();

        //解析模板
        return view('/admin/user/edit',['users'=>$users]);


    }

    //进行数据修改
    public function postUpdate(Request $request)
    {
        $data = $request->only('username','password','email','phone');
        //判断头像是否上传
        if($request->hasFile('pic')){
            $data['pic'] = $this->upload($request,'pic');
        }
        //获取id
        $id = $request->input('id'); 
        //进行数据修改操作
        $res = DB::table('user')->where('id',$id)->update($data);

        if($res){
            return redirect('/admin/user/index')->with('success','用户修改成功');
        }else{
            return back()->with('error','用户修改失败');
        }

    }

    //提升为普通管理员
    public function getUp(Request $request)
    {	
    	$data = $request->only('auth');
    	$data['auth'] = '普通管理员';
    	//获取id
    	$id = $request->input('id');
    	//数据修改操作
    	$res = DB::table('user')->where('id',$id)->update($data);
    	if($res){
            return redirect('/admin/user/index')->with('success','修改成功');
        }else{
            return back();
        }

    }


    //降为普通会员
    public function getDown(Request $request)
    {	
    	$data = $request->only('auth');
    	$data['auth'] = '普通会员';
        // dd($data['auth']);
    	//获取id
    	$id = $request->input('id');
    	//数据修改操作
    	$res = DB::table('user')->where('id',$id)->update($data);
        // dd($res);
    	if($res){
            return redirect('/admin/user/index')->with('success','修改成功');
        }else{
            return back();
        }

    }

}
